using System;
using System.Collections;
using System.Collections.Generic;
using System.Html;
using System.Runtime.CompilerServices;
using System.Serialization;
using Awaiter;
using Nsb;
using Nsb.Classes;
using jQueryApi;

namespace WhirliGig.Classes
{
    [IgnoreNamespace]
    public class EditRawGigs : Actor
    {
        private GigShowEdit _slides;
        private readonly jQueryObject _outer;
        private readonly jQueryObject _textarea;

        private EditRawGigs()
        {

            _outer = jQuery.FromHtml("<div id='EditRawGigs' class='AboveHider'/>")
                .AppendTo(Document.Body)
                ;

            _textarea = jQuery.FromHtml("<textarea wrap='off' style='font-family:courier new;font-size:8pt'/>")
                .AppendTo(_outer)
                ;

            _outer.Bind("keydown.esc", delegate(jQueryEvent e2) { Keys.EscKey(e2, Close); });
            _outer.Plugin<Jewl>().Position(new Dictionary("my", "top center", "at", "top center", "of", Window.Self));
            Keys.FilterAll(_outer);
            FocusElementAw(Await.AsyncAw, _textarea);
        }

        public static void Ev(jQueryEvent e, GigShowEdit gse)
        {
            CancelEvent(e);
            EditRawGigs erg = new EditRawGigs();
            erg._slides = gse;
            List<SlideData> sds = gse.Slides;
            string cssTxt = Rx.PrettyJson(sds, "  ");
            erg._textarea.Value(cssTxt);
            erg._textarea.Bind("keydown.shirtret", delegate(jQueryEvent e2) { Keys.ShiftRetKey(e2, erg.SaveGigsEv); });
        }

        private void SaveGigsEv(jQueryEvent e)
        {
            string txt = jQuery.Select("#EditRawGigs textarea").GetValue().Trim();
            try
            {
                List<SlideData> slides = (List<SlideData>)Json.Parse(txt);
                _slides.Slides = slides;
                new Await()
                    .AddAw(_slides.RestoreSlideAw, _slides.Slide)
                    .AddAw(_slides.SaveAllAw)
                    .Commit();
                Close(e);
            }
            catch (Exception ex)
            {
                Script.Alert("Error parsing json\n\n" + ex);
            }
        }

        public static void PasteBuffer(jQueryEvent e)
        {
            CancelEvent(e);
            EditRawGigs erg = new EditRawGigs();
            string txt = Rx.PrettyJson(Storage.GetLocal("CopyBuffer"), "  ");
            erg._textarea.Value(txt);
            erg._textarea.Bind("keydown.shiftret", delegate(jQueryEvent e2) { Keys.ShiftRetKey(e2, erg.SavePasteEv); });
        }

        private void SavePasteEv(jQueryEvent e)
        {
            string txt = jQuery.Select("#EditRawGigs textarea").GetValue().Trim();
            try
            {
                object tst = Json.Parse(txt);
                Storage.SetLocal("CopyBuffer", tst);
                Close(e);
            }
            catch (Exception ex)
            {
                Script.Alert("Error parsing json\n\n" + ex);
            }
        }

        private void Close(jQueryEvent e)
        {
            jQuery.Select("#EditRawGigs").Remove();
        }
    }
}